import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:image_picker/image_picker.dart';
import '../../helper/channelHelper.dart';
import '../../helper/upload_helper.dart';
import '../../model/space_item.dart';
import '../../spaces/image_space/record_list.dart';
import '../../spaces/movie_space/movie_play_path.dart';
import '../../helper/uidata.dart';
import 'package:photo_view/photo_view.dart';

import '../../helper/Helper.dart';

class MovRecordAdd2Page extends StatefulWidget {
  @override
  MovRecordAdd2Page(this.SpaceId, this.added, this.Ftype);
  final String SpaceId;
  final RecordAddCallback added;
  final int Ftype;

  @override
  MovRecordAdd2PageState createState() => new MovRecordAdd2PageState();
}

class MovRecordAdd2PageState extends State<MovRecordAdd2Page> {
  late File _img;
  bool upLoading = false;
  final picker = ImagePicker();

  Future getImg() async {
    final pickedFile = await picker.getVideo(source: ImageSource.camera);

    if (pickedFile != null) {
      setState(() {
        //_img = image2;
        _img = File(pickedFile.path);
      });
    } else {
      return;
    }
  }

  ///压缩

  Future upLoadImg() async {
    if (_img == null) return;
    var guid = Helper.GetGuid();
    var size1 = _img.lengthSync();
    var x1 = new SpaceItem(
        0,
        guid,
        UIData.CurrentUser!.Guid,
        widget.SpaceId,
        2,
        _img.path,
        Helper.DateTimeNowString(),
        -1,
        size1,
        "",
        "",
        "",
        "",
        "",
        "");

    var pb1 = await SpaceItem.Add(x1);
    if (!pb1) return;

    UploadHelper.UpLoad2(widget.Ftype, _img.path, "视频", guid, widget.SpaceId);

    //  Helper.fileCache.putFile(Helper.GetImgUrl(guid), _img.readAsBytesSync());

    if (pb1 == true) {
      Navigator.of(context).pop();
      // widget.added(x1);
    }

    ChannelHelper.showToast("已加入上传列表！");
  }

  @override
  initState() {
    super.initState();
    getImg();
  }

  @override
  void dispose() {
    //注意这里关闭
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    if (_img == null) return new Container();

    return new Scaffold(
      appBar: AppBar(
        title: Text("上传图片"),
      ),
      persistentFooterButtons: <Widget>[
        Text(
          Helper.GetFileSize(_img.lengthSync()),
          style: TextStyle(color: Colors.black38),
        ),
        ElevatedButton(
          // color: Colors.blue,
          //  textColor: Colors.white,
          child: Text("上传"),
          onPressed: () {
            upLoadImg();
          },
        )
      ],
      body: upLoading == true
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : new Container(
              child: _img == null
                  ? Center(child: Text("未选择图片"))
                  : FileMoviePlayPage(_img),
            ),
    );
  }
}
